package com.xuebusi.xpress;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PyDemoApp {

	public static void main(String[] args) {
//		demo1();
//		demo2();
		demo3();
	}

	private static void demo3() {
		String[] argsArr = new String[] { "python", "/Users/v_shiyanjun/baidu/ai/xpress/script/test/hello.py", "Python is best!", "Java invoke Python!"};
		try {
			Process proc = Runtime.getRuntime().exec(argsArr);// 执行py文件
			read(proc);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void demo2() {
		int a = 18;
		int b = 23;
		try {
			String[] argsArr = new String[] { "python", "/Users/v_shiyanjun/baidu/ai/xpress/script/test/demo2.py", String.valueOf(a), String.valueOf(b) };
			Process proc = Runtime.getRuntime().exec(argsArr);// 执行py文件

			read(proc);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private static void demo1() {
		Process proc;
		try {
			proc = Runtime.getRuntime().exec("python /Users/v_shiyanjun/baidu/ai/xpress/script/test/demo.py");// 执行py文件
			//用输入输出流来截取结果
			read(proc);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private static void read(Process proc) throws IOException, InterruptedException {
		BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
		String line;
		while ((line = in.readLine()) != null) {
			System.out.println(line);
		}
		in.close();
		proc.waitFor();
	}
}